<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="类型" prop="typeId">
        <el-form v-show="type_open" ref="type_form" :model="type_form" :rules="type_rules" label-width="0px">
          <el-form-item prop="name">
            <el-input ref="type_data_input" size="small" placeholder="请输入类型名称" v-model="type_form.name"
                      style="width: 15vw;">
              <div slot="prepend" style="text-align: center">
                <el-button type="text" icon="el-icon-circle-close" @click="cancelAddType"/>
              </div>
              <div slot="append" style="text-align: center">
                <el-button type="text" icon="el-icon-circle-check" @click="submitAddType"/>
              </div>
            </el-input>
          </el-form-item>
        </el-form>
        <div v-show="!type_open">
          <el-select ref="type_data_select" v-model="queryParams.typeId" clearable placeholder="请选择类型"
                     :loading="type_loading">
            <el-option v-for="item in jiayi_news_typeList" :key="item.id" :label="item.name" :value="item.id">
              <div style="display: flex;flex-direction: row;justify-content: space-between">
                <span>{{ item.name }}</span>
                <div>
                  <el-button icon="el-icon-edit" type="text" @click="handleUpdateType(item)"></el-button>
                  <el-button icon="el-icon-delete" type="text" @click="handleDeleteType(item)"></el-button>
                </div>
              </div>
            </el-option>
          </el-select>
          <el-tooltip class="item" effect="dark" content="添加类型" placement="top">
            <el-button type="info" plain icon="el-icon-plus" size="small" @click="handleAddType"
                       v-hasPermi="['cms:jiayi_news_type:add']"></el-button>
          </el-tooltip>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cms:jiayi_news:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['cms:jiayi_news:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['cms:jiayi_news:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getListPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jiayi_newsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="海报图" align="center" prop="poster" width="100">
        <template slot-scope="scope">
          <imagePreview :src="scope.row.poster" width="99%" height="100%"/>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="发布时间" align="center" prop="date"/>
      <el-table-column label="类型" align="center" prop="typeDTO.name"/>
      <el-table-column label="类型" align="center" prop="typeDTO.name"/>
      <el-table-column label="首页" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isHome" :active-value="1" :inactive-value="0" active-text="展示" inactive-text="不展示" @change="handleUpdateHome(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['cms:jiayi_news_typeedit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['cms:jiayi_news_type:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getListPage"/>

    <!-- 添加或修改新闻动态对话框 -->
    <el-drawer :title="title" :visible.sync="open" size="70%" direction="rtl" append-to-body destroy-on-close
               :wrapperClosable="false">
      <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
          <div class="form_flex">
            <section class="form_flex_left">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称" style="width: 90%" clearable/>
              </el-form-item>
              <el-form-item label="发布时间" prop="date">
                <el-date-picker v-model="form.date" type="datetime" value-format="yyyy-MM-dd hh:mm:ss"
                                placeholder="选择日期" style="width: 90%" clearable/>
              </el-form-item>
              <el-form-item label="类型" prop="typeId">
                <el-select v-model="form.typeId" placeholder="请选择类型" style="width: 90%" clearable>
                  <el-option v-for="item in jiayi_news_typeList" :key="item.id" :label="item.name"
                             :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="首页展示" prop="isHome">
                <el-switch v-model="form.isHome" :active-value="1"
                           :inactive-value="0" active-text="展示" inactive-text="不展示"></el-switch>
              </el-form-item>
              <el-form-item label="海报图" prop="poster">
                <ImageUpload :value="form.poster" :limit="1" @input="handleUploadPoster"/>
              </el-form-item>
              <el-form-item label="简述" prop="remark">
                <el-input v-model="form.remark"  type="textarea" placeholder="请输入名称" style="width: 90%" :rows="6"  maxlength="300" show-word-limit clearable/>
              </el-form-item>
            </section>
            <section class="form_flex_right">
              <el-form-item label="详细描述" prop="info">
                <editor v-model="form.info" height="70vh" style="border: 1px #DCDFE6 solid;width: 100%;"/>
              </el-form-item>
            </section>
          </div>
        </el-form>
      </div>
      <div style="width: 100%;text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  listPageJiayi_news,
  getJiayi_news,
  delJiayi_news,
  addJiayi_news,
  updateJiayi_news
} from "@/api/cms/jiayi_news";
import {
  addJiayi_news_type, delJiayi_news_type,
  getJiayi_news_type,
  listJiayi_news_type,
  updateJiayi_news_type
} from "@/api/cms/jiayi_news_type";
import {now, parseTime} from "@/utils/zzDouUtils/dateUtil";


export default {
  name: "Jiayi_news",
  data() {
    return {
      // 遮罩层
      loading: true,
      type_loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻动态表格数据
      jiayi_newsList: [],
      // 新闻动态类型表格数据
      jiayi_news_typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      type_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        typeId: null,
        info: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      type_form: {
        id: null,
        name: null,
      },
      type_rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.getListPage();
    this.getTypeListPage();
  },
  methods: {

    /** 查询新闻动态列表 */
    getListPage() {
      this.loading = true;
      listPageJiayi_news(this.queryParams).then(response => {
        this.jiayi_newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        typeId: null,
        poster: null,
        info: null,
        isHome: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加新闻动态";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getJiayi_news(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改新闻动态";
      });
    },
    /** 修改是否首页 */
    handleUpdateHome(row){
      let body = {id: row.id, isHome: row.isHome};
      updateJiayi_news(body).then(body => {
        this.$modal.msgSuccess("修改成功");
        this.getListPage();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateJiayi_news(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getListPage();
            });
          } else {
            addJiayi_news(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getListPage();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除新闻动态编号为"' + ids + '"的数据项？').then(function () {
        return delJiayi_news(ids);
      }).then(() => {
        this.getListPage();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //上传海报图
    handleUploadPoster(url) {
      this.form.poster = url;
    },
    //查询新闻动态类型列表
    getTypeListPage() {
      this.type_loading = true;
      listJiayi_news_type({}).then(response => {
        this.jiayi_news_typeList = response.data;
        this.type_loading = false;
      });
    },
    //添加类型
    resetTypeForm() {
      this.type_form = {
        id: null,
        name: null,
        date: parseTime(now()),
        isHome: 0,
      }
    },
    handleAddType() {
      this.resetTypeForm();
      this.type_open = true;
      this.checkTypeInput();
    },
    submitAddType() {
      this.$refs["type_form"].validate(valid => {
        if (valid) {
          if (this.type_form.id != null) {
            updateJiayi_news_type(this.form).then(response => {
              this.$modal.msgSuccess("修改类型成功");
              this.getTypeListPage();
              this.cancelAddType();
            });
          } else {
            addJiayi_news_type(this.type_form).then(response => {
              this.$modal.msgSuccess("新增类型成功");
              this.getTypeListPage();
              this.cancelAddType();
            });
          }
        }
      });
    },
    cancelAddType() {
      this.resetTypeForm();
      this.type_open = false;
    },
    //修改类型按钮操作
    handleUpdateType(row) {
      this.resetTypeForm();
      const id = row.id || this.ids
      getJiayi_news_type(id).then(response => {
        this.type_form = response.data;
        this.type_open = true;
        this.checkTypeInput();
        this.$set(this.queryParams, 'typeId', null);
      });
    },
    //删除类型按钮操作
    handleDeleteType(row) {
      this.$set(this.queryParams, 'typeId', null);
      const ids = row.id || this.ids;
      this.$modal.confirm(`是否确认删除新闻动态类型【${row.name}】`).then(function () {
        return delJiayi_news_type(ids);
      }).then(() => {
        this.getTypeListPage();
        this.$modal.msgSuccess("删除成功");
        this.$set(this.queryParams, 'typeId', null);
      }).catch(() => {
      });
    },
    //类型输入框获取焦点
    checkTypeInput() {
      this.$nextTick(() => {
        this.$refs.type_data_input.focus();
      })
    }
  }
};
</script>
<style>
.form_flex {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}

.form_flex_left {
  width: 40%;
  display: flex;
  flex-direction: column;
  justify-content: start;
}

.form_flex_button {
  flex: 1;
}
</style>
